package com.cty.service.impl;

import com.cty.mapper.UserMapper;
import com.cty.pojo.PageInfo;
import com.cty.pojo.User;
import com.cty.service.UserService;
import com.cty.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Auther: cty
 * @Date: 2020/3/3 12:42
 * @Description:
 * @version: 1.0
 */
public class UserServiceImpl implements UserService {
    @Override
    public PageInfo getPageData(int pageSize, int pageNum) {
        // 获取绑定接口的实体类
        SqlSession session = MyBatisUtil.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);

        // 将数据封装到PageInfo中
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);  // 每页pageSize条数据记录
        pageInfo.setPageNum(pageNum);  // 显示第pageNum页数据

        int dataTotal = userMapper.count();
        pageInfo.setPageTotal(dataTotal%pageSize==0?dataTotal/pageSize:dataTotal/pageSize+1);  // 数据表可分为pageTotal页

        List<User> pageList = userMapper.selByPage(pageSize*(pageNum-1), pageSize);
        pageInfo.setPageList(pageList);

        return pageInfo;
    }
}
